﻿using System.Data;
using KZ.Utilities;
using Process.Entities;
using KZ.Exception;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Process.DAL
{
    public class TransactionDAL
    {
        private static TransactionENT CreateTransactionFromReader(IDataReader reader)
        {
            TransactionENT item = new TransactionENT();

            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.TransID.ToString()))) item.TransID = Utility.StringParse(reader[TransactionColumns.TransID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.PartnerID.ToString()))) item.PartnerID = Utility.StringParse(reader[TransactionColumns.PartnerID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.ProductID.ToString()))) item.ProductID = Utility.StringParse(reader[TransactionColumns.ProductID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.ProductPrice.ToString()))) item.ProductPrice = Utility.DecimalParse(reader[TransactionColumns.ProductPrice.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.MinQuantity.ToString()))) item.MinQuantity = Utility.IntParse(reader[TransactionColumns.MinQuantity.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.NextTransQuantity.ToString()))) item.NextTransQuantity = Utility.IntParse(reader[TransactionColumns.NextTransQuantity.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.BeginTransDate.ToString()))) item.BeginTransDate = Utility.DateTimeParse(reader[TransactionColumns.BeginTransDate.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.EndTransDate.ToString()))) item.EndTransDate = Utility.DateTimeParse(reader[TransactionColumns.EndTransDate.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.TransStatus.ToString()))) item.TransStatus = Utility.StringParse(reader[TransactionColumns.TransStatus.ToString()]);

            DataTable dt = reader.GetSchemaTable();
            if (dt.IsColumnExist(TransactionColumns.PartnerName.ToString()))
            {
                if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.PartnerName.ToString()))) item.PartnerName = Utility.StringParse(reader[TransactionColumns.PartnerName.ToString()]);
            }
            if (dt.IsColumnExist(TransactionColumns.ProductName.ToString()))
            {
                if (!reader.IsDBNull(reader.GetOrdinal(TransactionColumns.ProductName.ToString()))) item.ProductName = Utility.StringParse(reader[TransactionColumns.ProductName.ToString()]);
            }

            return item;
        }

        public static TransactionENT GetTransactionByID(string transID)
        {
            try
            {
                TransactionENT trans = new TransactionENT();

                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, transID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procTransactionGetByID"))
                {
                    while (reader.Read())
                    {
                        trans = CreateTransactionFromReader(reader);
                    }
                }

                return trans;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static List<TransactionENT> GetTransactionList(string partnerID, TransStatus status)
        {
            try
            {
                List<TransactionENT> transList = new List<TransactionENT>();

                DataAccessHelper.AddParameter("PartnerID", SqlDbType.VarChar, partnerID);
                DataAccessHelper.AddParameter("TransStatus", SqlDbType.VarChar, status.ToString());

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procTransactionGetList"))
                {
                    while (reader.Read())
                    {
                        transList.Add(CreateTransactionFromReader(reader));
                    }
                }

                return transList;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy thông tin giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public bool InsertTransaction(TransactionENT transaction)
        {
            try
            {
                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, transaction.TransID);
                DataAccessHelper.AddParameter("PartnerID", SqlDbType.VarChar, transaction.PartnerID);
                DataAccessHelper.AddParameter("ProductID", SqlDbType.VarChar, transaction.ProductID);
                DataAccessHelper.AddParameter("MinQuantity", SqlDbType.Int, transaction.MinQuantity);
                DataAccessHelper.AddParameter("NextTransQuantity", SqlDbType.Int, transaction.NextTransQuantity);
                DataAccessHelper.AddParameter("BeginTransDate", SqlDbType.DateTime, transaction.BeginTransDate);
                DataAccessHelper.AddParameter("EndTransDate", SqlDbType.DateTime, transaction.EndTransDate);
                DataAccessHelper.AddParameter("TransStatus", SqlDbType.VarChar, transaction.TransStatus);

                if (DataAccessHelper.ExecuteNonQuery("procTransactionInsert") > 0)
                {
                    return true;
                }
                throw new DataAccessException("Có lỗi trong quá trình thêm mới giao dịch.");
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình thêm mới giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public bool UpdateTransactionStatus(string transID, TransStatus transStatus)
        {
            try
            {
                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, transID);
                DataAccessHelper.AddParameter("TransStatus", SqlDbType.VarChar, transStatus.ToString());

                if (DataAccessHelper.ExecuteNonQuery("procTransactionUpdateStatus") > 0)
                {
                    return true;
                }
                throw new DataAccessException("Có lỗi trong quá trình cập nhật trạng thái giao dịch.");
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình cập nhật trạng thái giao dịch.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}